/**
  ******************************************************************************
  * @file    main.c
  * @author  MCU Application Team
  * @brief   Main program body
  * @date
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
  * All rights reserved.</center></h2>
  *
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "malloc.h"
#include "gpio.h"
#include "spi.h"
#include "lora_thread.h"
#include "heartbeat.h"
#include "Switch.h"
#include "config.h"
#include "usart.h"
#include "Uart_thread.h"
/* Private define ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private user code ---------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void Error_Handler(void);

IWDG_HandleTypeDef   IwdgHandle;

#define MEM1_BLOCK_SIZE 32                             //内存块大小为32字节
#define MEM1_MAX_SIZE 1 * 512                         //内存池大小
__align(32) uint8_t mem1base[MEM1_MAX_SIZE];           //内存池
uint16_t mem1mapbase[MEM1_MAX_SIZE / MEM1_BLOCK_SIZE]; //内存管理表
Mallco_dev mallco_dev;                                 //内存管理器

void PY_SPI_Init()
{
	/*初始化SPI配置*/
  hspi1.Instance               = SPI1;                       /* SPI1 */
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;  /* 128分频 */
  hspi1.Init.Direction         = SPI_DIRECTION_2LINES;       /* 全双工 */
  hspi1.Init.CLKPolarity       = SPI_POLARITY_LOW;           /* 时钟极性低 */
  hspi1.Init.CLKPhase          = SPI_PHASE_1EDGE ;           /* 数据采样从第一个时钟边沿开始 */
  hspi1.Init.DataSize          = SPI_DATASIZE_8BIT;          /* SPI数据长度为8bit */
  hspi1.Init.FirstBit          = SPI_FIRSTBIT_MSB;           /* 先发送MSB */
  hspi1.Init.NSS               = SPI_NSS_SOFT;               /* NSS软件模式 */
  hspi1.Init.Mode = SPI_MODE_MASTER;                         /* 配置为主机 */
  if (HAL_SPI_DeInit(&hspi1) != HAL_OK)                      /* SPI反初始化 */
  {
    Error_Handler();
  }
  /*SPI初始化*/
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
}
void  IWDG_Init()
{
	__HAL_RCC_LSI_ENABLE();
  IwdgHandle.Instance = IWDG;                     /* 选择IWDG */
  IwdgHandle.Init.Prescaler = IWDG_PRESCALER_128;  /* 配置32分频 */
  IwdgHandle.Init.Reload = (5000);                /* IWDG计数器重装载值为1000，5s */

  if (HAL_IWDG_Init(&IwdgHandle) != HAL_OK)        /* 初始化IWDG */
  {

    Error_Handler();
  }
}
/**
  * @brief  应用程序入口函数.
  * @retval int
  */
int main(void)
{
  /* 初始化所有外设，Flash接口，SysTick */
	uint16_t iwdg_time=0;
  HAL_Init();
	MX_GPIO_Init();
	MX_SPI1_Init();
	MX_USART1_UART_Init();
	my_mem_init(&mallco_dev, mem1base, mem1mapbase, MEM1_MAX_SIZE, MEM1_BLOCK_SIZE);
	IWDG_Init();   //初始化看门狗
	Init_Model();
	Lora_Init();
	Heardbeat_Init();
	Switch_Init();
	USART_Init();
  while (1)
  {
		lora_loop(20);
		Heartbeat_Loop(20);
		Switch_Loop(20);
		USART_Loop(20);
    HAL_Delay(20);                          /* 延时250ms */
		iwdg_time+=20;
		if(iwdg_time>2000)
		{
			iwdg_time=0;
			if (HAL_IWDG_Refresh(&IwdgHandle) != HAL_OK)
      {
       Error_Handler();
      }
			
		}
  }
}

/**
  * @brief  错误执行函数
  * @param  无
  * @retval 无
  */
void Error_Handler(void)
{
  /* 无限循环 */
  while (1)
  {
  }
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  输出产生断言错误的源文件名及行号
  * @param  file：源文件名指针
  * @param  line：发生断言错误的行号
  * @retval 无
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* 用户可以根据需要添加自己的打印信息,
     例如: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* 无限循环 */
  while (1)
  {
  }
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT Puya *****END OF FILE****/
